<?php
/**
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * This license is also available via the world-wide-web at
 * http://itdapps.ncsu.edu/bsd.txt
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to itappdev@ncsu.edu so we can send you a copy immediately.
 *
 * @package    Loaner_Item
 * @copyright  Copyright (c) 2007 NC State University Information Technology Division
 * @license    http://itdapps.ncsu.edu/bsd.txt  BSD License
 * @author     Jason Austin <jason_austin@ncsu.edu>
 * @author     Garrison Locke <garrison_locke@ncsu.edu>
 * @see        http://itdapps.ncsu.edu
 * @version    SVN: $Id: ActionLog.php 155 2007-07-19 19:44:26Z jfaustin@EOS.NCSU.EDU $
 */

/**
 * @package    Tasks
 * @category   Model
 * @copyright  Copyright (c) 2007 NC State University Information Technology Division
 *
 */
class Tasks extends Ot_Db_Table 
{
    /**
     * Database table name
     *
     * @var string
     */
    public $_name = 'tbl_tasks';

    /**
     * Primary key for the database
     *
     * @var string
     */
    protected $_primary = 'taskId';
   

    public function getTasksForAccountId($accountId)
    {
    	$where = $this->getAdapter()->quoteInto('accountId = ?', $accountId);
	
	    return $this->fetchAll($where)->toArray();
    }
    
	public function isTaskOwner($accountId, $taskId)
    {
    	$dba = $this->getAdapter();
        $where = $dba->quoteInto('taskId = ?', $taskId);
        $where .= ' AND ' . $dba->quoteInto('accountId = ?', $accountId);
        $result = $this->fetchAll($where); 
        return ($result->count() == 1);
    }
    
    public function form(array $default = array())
    {
    	$form = new Zend_Form();
    	$form->setAction('')
             ->setMethod('post')
             ->setAttrib('id', 'loanerForm')
             ->setDecorators(array(
                 'FormElements',
                 array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form')),
                 'Form',
             ));
                
        $tags = new Tags();
        $allTags = $tags->fetchAll()->toArray();
        
        $tagOptions = array();
        foreach($allTags as $t) {
        	$tagOptions[$t['tagId']] = $t['name'];
        }
        
             
        $tagId = $form->createElement('select', 'parentTagId', array('label' => 'Tag: '));
        $tagId->setRequired(false);
        $tagId->setMultiOptions($tagOptions);
        $tagId->setValue((isset($default['tagId'])) ? $default['tagId'] : '');
             
        $form->addElements(array($tagId));
       
             
       	$taskName = $form->createElement('text', 'taskName', array('label' => 'Task Name:'));
        $taskName->setRequired(true)
             ->addFilter('StringTrim')
             ->addFilter('StripTags')
             ->setAttrib('maxlength', '255')
             ->setAttrib('size', '75')
             ->setValue((isset($default['taskName'])) ? $default['taskName'] : '');
             
        $description = $form->createElement('textarea', 'description', array('label' => 'Description:'));
        $description->addFilter('StringTrim')
              ->addFilter('StripTags')
              ->setAttrib('style', 'width: 400px; height: 75px;')
              ->setAttrib('id', 'wysiwyg-tasks')
              ->setValue((isset($default['description'])) ? $default['description'] : '');     
             
        $form->addElements(array($taskName, $description))
             ->setElementDecorators(array(
                     'ViewHelper',
                     'Errors',      
                     array('HtmlTag', array('tag' => 'div', 'class' => 'elm')), 
                     array('Label', array('tag' => 'span')),      
             ));
                     
        
        $submit = $form->createElement('submit', 'saveButton', array('label' => 'Save Task'));
        $submit->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formSubmit'))
                 ));
        
        $cancel = $form->createElement('button', 'cancel', array('label' => 'Cancel'));
        $cancel->setAttrib('id', 'cancel');
        $cancel->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formButton'))
                ));        
                            
        $form->setElementDecorators(array(
                     'ViewHelper',
                     'Errors',      
                     array('HtmlTag', array('tag' => 'div', 'class' => 'elm')), 
                     array('Label', array('tag' => 'span')),      
             ))
             ->addElements(array($submit, $cancel));
             
        return $form;
    }
    
	public function deleteTaskForm(array $default = array())
    {
    	$form = new Zend_Form();
    	$form->setAction('')
             ->setMethod('post')
             ->setAttrib('id', 'deleteTaskForm')
             ->setDecorators(array(
                 'FormElements',
                 array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form')),
                 'Form',
             ));

        $taskName = $form->createElement('text', 'taskName', array('label' => 'Task Name:'));
        $taskName->setRequired(true)
             ->addFilter('StringTrim')
             ->setAttrib('readonly', true)
             ->setValue((isset($default['taskName'])) ? $default['taskName'] : '');
             
        $form->addElements(array($taskName))
             ->setElementDecorators(array(
                     'ViewHelper',
                     'Errors',      
                     array('HtmlTag', array('tag' => 'div', 'class' => 'elm')), 
                     array('Label', array('tag' => 'span')),      
        ));
             
        $submit = $form->createElement('submit', 'saveButton', array('label' => 'Delete Task Now'));
        $submit->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formSubmit'))
                 ));
        
        $cancel = $form->createElement('button', 'cancel', array('label' => 'Cancel'));
        $cancel->setAttrib('id', 'cancel');
        $cancel->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formButton'))
                ));        
                            
        $form->setElementDecorators(array(
                     'ViewHelper',
                     'Errors',      
                     array('HtmlTag', array('tag' => 'div', 'class' => 'elm')), 
                     array('Label', array('tag' => 'span')),      
             ))
             ->addElements(array($submit, $cancel));
             
        $taskId = $form->createElement('hidden', 'taskId');
        $taskId->setValue($default['taskId']);
        $taskId->setDecorators(array(
            array('ViewHelper', array('helper' => 'formHidden'))
        ));
        
        $form->addElements(array($taskId));
             
        return $form;
    }
    
}